# 战争迷雾系统

import iworld2d
import math3d

# 小迷雾块大小
fogsize = 30

# 累计器
visionsum = 0
# 迷雾数量
h = 768/fogsize+1
w = 1024/fogsize+1


# 视野
class tgame_vision(object):
	def __init__(self, vision = 20, pos = (0,0) , mode = "pos", name = ""):
		# 累计器加1
		global visionsum
		visionsum += 1
		# 视野范围
		self.vision = vision
		# 模式 ui:固定UI上 pos:世界坐标
		#self.mode = mode
		# 坐标
		self.pos = pos
		# 如果没有名称,则自动创建
		if name == "":
			name = "view_"+str(visionsum)
		self.name = name
		# 不可见空实体
		self.obj = None

		#return self.name

foglist = {}
fogent = {}
visions = {}
# 摄像机坐标不变则不更新坐标
last_cx = 0
last_cy = 0

# 战争迷雾主要管理类
def init():
	# 全屏战争迷雾表
	# 数值为1 表示有迷雾,初始化,全屏迷雾
	global foglist,fogent
	foglist = [[1 for col in range(h)] for row in range(w)]
	# 加载所有迷雾
	for y in range(0, h):
		for x in range(0, w):
			# 加载迷雾,迷雾层为4
			index = y*w+x
			fogent[index] = iworld2d.image2d( texture_file = "tgame/res/world2d/txg/fog.png", layer_id = 4 , name = "_fog_" + str(index))
			fogent[index].pos = (x * fogsize + 500, y * fogsize + 500)


def add_vision(vision , ent):
	# 视野是是圆形的,用一个圆形的图形来代表,视野值为此圆形的倍数.用图片可直接与场景中的雾作碰撞测试
	vision.obj = iworld2d.image2d( texture_file = "tgame/res/world2d/txg/fog_obj.png", layer_id = 4 , name = vision.name)
	vision.obj.set_pick()
	vision.obj.hide()
	vision.ent = ent
	global visions
	visions[vision.name] = vision

	# 显示视野中的迷雾
def show_fog(vision):
	#print "showfog"
	# 以视野中心为中间,计算所有迷雾到中心的距离,如果在范围内,则隐藏其迷雾
	for y in range(0, h):
			for x in range(0, w):
				fog = fogent[y*w+x]
				#print "ddd"
				if fog.is_hide() == False:
					distance = math3d.vector2(vision.pos[0],vision.pos[1]) - math3d.vector2(fog.pos[0],fog.pos[1])
					#hide = False
					#print "ddd"
					if distance.length < (vision.vision * 30)/2:
						fog.hide()
					elif distance.length < (vision.vision * 35)/2:
						fog.alpha = 90
					elif distance.length < (vision.vision * 40)/2:
						fog.alpha = 180

	# 根据当前屏幕视野,更新迷雾状态,透明度,是否显示
def update_fog():
	global fogent
	# 显示所有迷雾
	for y in range(0, h):
		for x in range(0, w):
			fogent[y*w+x].show()
			fogent[y*w+x].alpha = 255

	# 获取当前屏幕范围内的可见视野
	camera_x,camera_y = iworld2d.camera_to_worldpos(0,0)
	objlist = iworld2d.select_with_rect(False, camera_x, camera_y, camera_x+1024, camera_y+768)

	#print objlist
	if objlist:
		# 获得了所有的视野,再以视野判断
		for obj in objlist:
			# 名称为view_开关的为视野
			if len(obj.name) > 5 and obj.name[0:5] == "view_":
				# 得到视野,进行迷雾开启
				show_fog(visions[obj.name])

# 更新视野
def update_view():
	global visions
	for view in visions:
		viewobj = visions[view]
		#print "objpos:", viewobj.ent.pos
		# 坐标
		if viewobj.ent.pos != viewobj.pos:
			viewobj.pos = viewobj.ent.pos
			viewobj.obj.pos = viewobj.ent.pos
		# 视野
		#if viewobj.obj.scale != (viewobj.vision, viewobj.vision)
		viewobj.obj.scale = (viewobj.vision, viewobj.vision)
		#print viewobj.obj.scale
		pass

	# 迷雾坐标更新
def update_pos():
	global fogent,last_cx,last_cy
	# 更新所有迷雾的坐标
	camera_x,camera_y = iworld2d.camera_to_worldpos(0,0)
	if camera_x != last_cx or camera_y != last_cy:
		last_cx = camera_x
		last_cy = camera_y
		# 加载所有迷雾
		for y in range(0, h):
			for x in range(0, w):
				# 加载迷雾,迷雾层为4
				fogent[y*w+x].pos = (x * fogsize +camera_x, y * fogsize +camera_y)


def update():
	update_pos()
	update_view()
	update_fog()
